package com.secondhand.project.service.impl;

import com.secondhand.common.utils.SecurityUtils;
import com.secondhand.project.domain.Collect;
import com.secondhand.project.mapper.CollectMapper;
import com.secondhand.project.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl implements ICollectService {
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public Collect selectCollectById(Long id) {
        return collectMapper.selectCollectById(id);
    }

    @Override
    public List<Collect> selectCollectList(Collect collect) {
        return collectMapper.selectCollectList(collect);
    }

    @Override
    public int insertCollect(Collect collect) {
        collect.setUserId(SecurityUtils.getUserId());
        List<Collect> dbCollectList = collectMapper.selectCollectList(collect);
        if (!dbCollectList.isEmpty()) {//已收藏
            return collectMapper.deleteCollectById(dbCollectList.get(0).getId());
        } else {
            return collectMapper.insertCollect(collect);
        }
    }

    @Override
    public int updateCollect(Collect collect) {
        return collectMapper.updateCollect(collect);
    }

    @Override
    public int deleteCollectByIds(Long[] ids) {
        return collectMapper.deleteCollectByIds(ids);
    }

    @Override
    public int deleteCollectById(Long id) {
        return collectMapper.deleteCollectById(id);
    }
}
